
import { Button, Modal } from 'antd';
import { inject, observer } from "mobx-react";
import { toJS } from 'mobx';
import { WeaDialog, WeaFormItem, WeaSearchGroup, WeaNewScroll, WeaRightMenu } from 'ecCom';
import { WeaSwitch } from 'comsMobx';
import WeaCptTableEdit from '../CapitalTableEdit/WeaCptTableEdit'
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@observer
class InstockInfoDialog extends React.Component {
    constructor(props) {
        super(props);
    }
    componentDidMount(){
    }
    componentWillReceiveProps(nextProps) {
   
    }
    
    render() {
        const { instockInfoStore } = this.props.contentStore;
        const { visible, title, saveSubListData, subInfo, canEdit, subListData, batchBrowser } = instockInfoStore;
        return (
            <div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@5o8kim`}
                    title={title}
                    visible={visible}
                    buttons={this.getBottomButtons()}
                    icon="icon-coms-fa"
                    iconBgcolor="#f14a2d"
                    onCancel={() => { instockInfoStore.handleDialog(false) }}
                    style={{ width: 1020, height: 520 }}
                >
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@r76fxw`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}  >
                        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@vnmy77`} height={"100%"}>
                            {this.getFormFields()}
                            <WeaCptTableEdit ecId={`${this && this.props && this.props.ecId || ''}_WeaCptTableEdit@dezi5b`}
                                ref={ref => {instockInfoStore.instockRemind = ref}}
                                title={getLabel(83579,"入库明细")}
                                showGroup={true}
                                needAdd={canEdit}
                                needCopy={true}
                                columns={toJS(subInfo.columnData || {})}
                                datas={toJS(subListData)}
                                datas_0={[]}
                                onChange={saveSubListData}
                                batchBrowser={batchBrowser}
                            />
                        </WeaNewScroll>
                    </WeaRightMenu>
                </WeaDialog>

            </div>)
    }
    getRightMenu() {
        const { instockInfoStore } = this.props.contentStore;
        const { rightMenu } = instockInfoStore;
        let btnArr = [];
        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            btnArr.push({
                icon: <i className={m.menuIcon} />,
                content: m.menuName
            })
        });
        return btnArr
    }
    onRightMenuClick(key) {
        const { contentStore } = this.props;
        const { instockInfoStore } = this.props.contentStore;
        const { rightMenu, instockid } = instockInfoStore;
        let that = this;
        rightMenu && rightMenu.length > 0 && rightMenu.map((m, i) => {
            if (Number(key) == i) {
                if (m.type == "BTN_SAVE") { //提交
                    instockInfoStore.doInstock(contentStore);
                } else if (m.type == "BTN_REJECT") { //重新申请
                    instockInfoStore.handleDialog(true, { instockid: instockid }, -1, 2);
                } else if (m.type == "BTN_BACK") { //收回
                    Modal.confirm({
                        title: getLabel(15172,'系统提示'),
                        content:  getLabel(83583,'您确定将此入库单收回吗?'),
                        onOk() {
                            instockInfoStore.doRejectInstock({ 'instockid': instockid }, contentStore);
                        },
                        onCancel() { },
                    });
                }
            }
        });
    }

    getBottomButtons = () => {
        const { contentStore } = this.props;
        const { instockInfoStore } = this.props.contentStore;
        const { rightMenu, instockid } = instockInfoStore;
        let btnArr = [];
        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            m.isTop == '1' && btnArr.length < 4 && btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@q30cfj@${m.type}`} type="primary"
                    onClick={() => {
                        if (m.type == "BTN_SAVE") { //提交
                            instockInfoStore.doInstock(contentStore);
                        } else if (m.type == "BTN_REJECT") { //重新申请
                            instockInfoStore.handleDialog(true, { instockid: instockid }, -1, 2);
                        } else if (m.type == "BTN_BACK") { //收回
                            Modal.confirm({
                                title: getLabel(15172,'系统提示'),
                                content:  getLabel(83583,'您确定将此入库单收回吗?'),
                                onOk() {
                                    instockInfoStore.doRejectInstock({ 'instockid': instockid }, contentStore);
                                },
                                onCancel() { },
                            });
                        }
                    }}>
                    {m.menuName}
                </Button>
            );
        });
        btnArr.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@e70vsv@cancel`} type="primary" onClick={() => { instockInfoStore.handleDialog(false) }}>{getLabel(309,'关闭')}</Button>)
        return btnArr;
    }
    getFormFields() {
        const { instockInfoStore } = this.props.contentStore;
        const { basicInfo, instockForm, canEdit } = instockInfoStore;
        const { isFormInit } = instockForm;
        let group = [];
        isFormInit && basicInfo.map((c,i) => {
            let items = [];
            c.items.map(fields => {
                let hide = false;
                let hasunderline = false;
                if (fields.viewAttr == "1" || (fields.conditionType == "BROWSER" && fields.browserConditionParam.viewAttr == "1")) { hasunderline=true; }  //只读样式控制
                if(hasunderline){
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@c6xlsu@${fields.domkey[0]}`}
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} underline>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@e0047i@${fields.domkey[0]}`} fieldConfig={fields} form={instockForm} />
                        </WeaFormItem>),
                        colSpan: 1,
                        hide: hide
                    });
                }else{
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@qawcs4@${fields.domkey[0]}`}
                            error={instockForm.getError(fields)}
                            tipPosition="bottom"
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} >
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@z1nqdp@${fields.domkey[0]}`} fieldConfig={fields} form={instockForm} />
                        </WeaFormItem>),
                        colSpan: 1,
                        hide: hide
                    });
                }
                
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@mwogkh@${i}`} needTigger={true} title={c.title} col={2} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }

}

export default InstockInfoDialog;